package org.se.scut.pinterest.mina.handler;

import java.util.List;

import org.apache.log4j.Logger;

import org.apache.mina.core.session.IoSession;
import org.se.scut.hibernate.dao.model.Picture;
import org.se.scut.hibernate.dao.service.PictureServiceImp;
import org.se.scut.hibernate.dao.service.iface.IPictureService;
import org.se.scut.pinterest.mina.codec.msg.Message;
import org.se.scut.pinterest.mina.codec.msg.MessageTypeEnum;
import org.se.scut.pinterest.mina.codec.msg.PicturesGetMessage;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessage;
import org.se.scut.pinterest.mina.codec.msg.PicturesPostMessageItem;
import org.se.scut.pinterest.mina.handler.observer.IMessageObserver;
import org.se.scut.pinterest.mina.handler.observer.MessageOperatorType;
import org.se.scut.pinterest.mina.utils.FileUtils;
import org.se.scut.pinterest.mina.utils.FormateUtils;

public class PicturesGetHandler implements IMessageObserver {
	
	private IPictureService picService = new PictureServiceImp();
	
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(PicturesGetHandler.class);

	@Override
	public void onMessageChange(IoSession session, Message msg,
			MessageOperatorType type) {
		PicturesGetMessage getMsg = (PicturesGetMessage)msg;
		logger.info("symbol: " + getMsg.getSymbol());
		logger.info("userID: " + getMsg.getUserID());
		logger.info("ablumID: " + getMsg.getAblumID());
		logger.info("pictureTypeID: " + getMsg.getPictureTypeID());
		logger.info("from: " + getMsg.getFrom());
		logger.info("count: " + getMsg.getCount());
		String symbol = getMsg.getSymbol();
		PicturesPostMessage postMsg = new PicturesPostMessage();
		copyBaseInfo(postMsg, getMsg);
		
		switch (MessageTypeEnum.getEnumBySybol(symbol)) {
		case PICTURESBYTYPE :
			session.write(handlePBT(postMsg, getMsg.getPictureTypeID(),
					getMsg.getFrom(), getMsg.getCount()));
			break;
		default:
			break;
		}
	}
	
	public PicturesPostMessage handlePBT(PicturesPostMessage postMsg, int typeID, int from, int count){
		List<Picture> pictures = picService.getPicturesByType(typeID, from, count);
		for (Picture picture : pictures) {
			PicturesPostMessageItem item = new PicturesPostMessageItem();
			item.setCreateTime(FormateUtils.formateDate(picture.getCreateTime()));
			item.setFileName(picture.getFilePath());
			byte[] content = new byte[picture.getContent_length()];
			FileUtils.fileToBytes(picture.getFilePath(), content);
			item.setContent(content);
			item.setId(picture.getId());
			item.setNumOfCollected(picture.getNumOfLove());
			item.setOverView(picture.getOverView());
			item.setPictureTypeID(picture.getPictureType().getId());
			item.setFileLength(picture.getContent_length());
			postMsg.getItems().add(item);
		}
		return postMsg;
	}
	
	private void copyBaseInfo(PicturesPostMessage postMsg, PicturesGetMessage getMsg){
		postMsg.setAblumID(getMsg.getAblumID());
		postMsg.setCount(getMsg.getCount());
		postMsg.setFrom(getMsg.getFrom());
		postMsg.setPictureTypeID(getMsg.getPictureTypeID());
		postMsg.setUserID(getMsg.getUserID());
	}

}
